home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ian.johnston@ubs.com
- From: ian.johnston@ubs.com (Ian Johnston (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: Creating an object via new with ONLY a pointer to the object
- Date: 11 Apr 1996 08:28:45 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4kifrt$lk1@ubszh.fh.zh.ubs.com>
- References: <4kh07v$lno@crchh327.rich.bnr.ca>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <4kh07v$lno@crchh327.rich.bnr.ca>, jobell@bnr.ca (Bret Bieghler) writes:
- |> An interesting problem I've come across... I was wondering if this
- |> is possible:
- |>
- |> I have a generic CommandObject which defines several pure virtual
- |> functions. Derived from CommandObject are user commands, such
- |> as ExitCommand, StatusCommand, etc.
- |>
- |> To process a command (currently) I do the following:
- |>
- |> CommandObject* basePtr;
- |>
- |> if (command == "exit")
- |> {
- |> basePtr = new ExitCommand;
- |> basePtr->implement();
- |> }
- |> else if (command == "status")
- |> {
- |> basePtr = new StatusCommand;
- |> basePtr->implement();
- |> }
- |>
- |> What I would LIKE to do is the the following:
- |>
- |> CommandObject* basePtr = new commandTable[command];
- |>
- |> where commandTable is an associative array as follows:
- |>
- |> Key Value
- |> "exit" ExitCommand*
- |> "status" StatusCommand*
-
- Give each CommadObject class a *static* function:
-
- class ExitCommand : public CommandObject
- {
- // ...
- static CommandObject *makeNew();
- };
-
- CommandObject *ExitCommand::makeNew()
- {
- return new ExitCommand;
- }
-
-
- Now you can make a table (assoc array, whatever) of strings and pointers
- to these functions.
-
- typedef CommandObject *(*MakeCmdObj)();
-
- MakeCmdObj funcptr = commandTable[command];
- CommandObject *baseptr = (*funcptr)();
-
-
- Ian
-